<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Resolution_model extends WS_model
{
    private function wallpaperResCount_sql()
    {
        $this->db->select("wallpaper_height, wallpaper_width, CONCAT(wallpaper_width,'x',wallpaper_height) AS wh, count(*) AS count,max(wallpaper_date_created) as last_created, max(wallpaper_date_modified) as last_modified",false);
        $this->db->from('wallpapers');
        $this->db->where('wallpaper_status', '1');
        $this->db->group_by('wh');
        $this->db->order_by('wallpaper_height', 'desc');
        $this->db->order_by('wallpaper_width', 'desc');
        return $this->sql();
    }

    public function get_resolutionsWithCount($onlyAvailable = false)
    {
        $wallpaper_countSQL = $this->wallpaperResCount_sql();
        $this->db->select("r.*,sum(w.count) as count,w.last_created,w.last_modified,rg.*,CONCAT(r.resolution_group_seq_id,'x',r.resolution_width,'x',r.resolution_height) AS rwh", false);
        $this->db->from('resolutions r');
        $this->db->join("($wallpaper_countSQL) w", 'w.wallpaper_height >= r.resolution_height AND w.wallpaper_width >= r.resolution_width', 'left');
        $this->db->join('resolutions_groups rg', 'rg.resolution_group_seq_id = r.resolution_group_seq_id', 'left');
        if ($onlyAvailable) {
            $this->db->where('w.wallpaper_height IS NOT NULL');
            $this->db->where('w.wallpaper_width IS NOT NULL');
        }
        $this->db->group_by('rwh');
        $this->db->order_by('rg.resolution_group_sort', 'asc');
        $this->db->order_by('r.resolution_width', 'asc');
        $this->db->order_by('r.resolution_height', 'asc');
        return $this->result();
    }

    public function get_AllResolutions($filter = array())
    {
        $count = (isset($filter['count']) ? TRUE : FALSE);
        $perPage = (isset($filter['per_page']) ? $filter['per_page'] : FALSE);
        $offset = (isset($filter['offset']) ? $filter['offset'] : FALSE);

        $this->db->from('resolutions r');
        $this->db->join('resolutions_groups rg', 'rg.resolution_group_seq_id = r.resolution_group_seq_id', 'left');
        $this->db->order_by('resolution_group_sort', 'asc');
        $this->db->order_by('resolution_width', 'asc');
        $this->db->order_by('resolution_height', 'asc');

        if (isset($filter['status_rg'])) {
            $this->db->where('rg.resolution_group_status', $filter['status_rg']);
        }
        if (isset($filter['status'])) {
            $this->db->where('r.resolution_status', $filter['status']);
        }
        if (isset($filter['name'])) {
            $this->db->like('r.resolution_name', $filter['name']);
            $this->db->or_like('r.resolution_width', $filter['name']);
            $this->db->or_like('r.resolution_height', $filter['name']);
            $this->db->or_like('rg.resolution_group_name', $filter['name']);
        }
        if ($count) {
            return $this->db->count_all_results();
        }

        if ($perPage)
            $this->db->limit($perPage, $offset);

        return $this->result();
    }

    public function get_AllResolutionsGroup($filter = array())
    {
        $count = (isset($filter['count']) ? TRUE : FALSE);
        $perPage = (isset($filter['per_page']) ? $filter['per_page'] : FALSE);
        $offset = (isset($filter['offset']) ? $filter['offset'] : FALSE);

        $this->db->from('resolutions_groups rg');
        $this->db->order_by('resolution_group_sort', 'asc');

        if (isset($filter['status'])) {
            $this->db->like('rg.resolution_group_status', $filter['status']);
        }
        if (isset($filter['name'])) {
            $this->db->like('rg.resolution_group_name', $filter['name']);
        }
        if ($count) {
            return $this->db->count_all_results();
        }
        if ($perPage)
            $this->db->limit($perPage, $offset);
        return $this->result();
    }

    private function get_groupBY($where = array())
    {
        $this->db->from('resolutions_groups');
        if (!empty($where)) {
            foreach ($where as $key => $value) {
                $this->db->where($key, $value);
            }
        }
        return $this->row();
    }

    private function get_resolutionBY($where = array())
    {
        $this->db->from('resolutions r');
        $this->db->join('resolutions_groups rg', 'rg.resolution_group_seq_id = r.resolution_group_seq_id');
        if (!empty($where)) {
            foreach ($where as $key => $value) {
                $this->db->where($key, $value);
            }
        }
        return $this->row();
    }

    //Resolutions

    public function get_resolutionBYWidthHeight($width, $height)
    {
        $where = array(
            'r.resolution_width' => $width,
            'r.resolution_height' => $height,
        );
        return $this->get_resolutionBY($where);
    }

    public function get_resolutionByID($id)
    {
        $where = array(
            'r.resolution_seq_id' => $id
        );
        return $this->get_resolutionBY($where);
    }

    //GROUP

    public function get_groupByName($name)
    {
        $where = array(
            'LOWER(resolution_group_name)' => strtolower($name)
        );
        return $this->get_groupBY($where);
    }

    public function get_groupByID($id)
    {
        $where = array(
            'resolution_group_seq_id' => $id
        );
        return $this->get_groupBY($where);
    }

    public function get_groupDropDown($exclude = array())
    {
        if (!is_array($exclude))
            $exclude = array($exclude);
        $res_group = $this->get_AllResolutionsGroup();
        if (!$res_group)
            return false;
        $return = array();
        foreach ($res_group as $r) {
            if (!in_array($r->resolution_group_seq_id, $exclude))
                $return[$r->resolution_group_seq_id] = $r->resolution_group_name;
        }
        return $return;
    }

    public function add($data, $batch = false)
    {
        if (!$batch) {
            $this->db->insert('resolutions', $data);
            return $this->db->insert_id();
        } else {
            $this->db->insert_batch('resolutions', $data);
        }
    }

    public function add_group($data)
    {
        $this->db->insert('resolutions_groups', $data);
        return $this->db->insert_id();
    }

    public function update_group($data, $id)
    {
        $this->db->update('resolutions_groups', $data, array('resolution_group_seq_id' => $id));
        return $this->db->affected_rows();
    }

    public function update($data, $id)
    {
        $this->db->update('resolutions', $data, array('resolution_seq_id' => $id));
        return $this->db->affected_rows();
    }

    public function delete_group($ids)
    {
        if (!is_array($ids)) {
            $this->db->delete('resolutions_groups', array('resolution_group_seq_id' => $ids));
            $this->db->delete('resolutions', array('resolution_group_seq_id' => $ids));
        } else {
            $this->db->where_in('resolution_group_seq_id', $ids);
            $this->db->delete('resolutions_groups');

            $this->db->where_in('resolution_group_seq_id', $ids);
            $this->db->delete('resolutions');
        }
        return $this->db->affected_rows();
    }

    public function delete($ids)
    {
        if (!is_array($ids)) {
            $this->db->delete('resolutions', array('resolution_seq_id' => $ids));
        } else {
            $this->db->where_in('resolution_seq_id', $ids);
            $this->db->delete('resolutions');
        }
        return $this->db->affected_rows();
    }
}